import os, sys
import pygame
from pygame.locals import *

#from ooPlayer import *    # if we want the OO version
from funcPlayer import * # if we want the Functional version

RED_WINE   = (60, 0, 0)
LIGHT_GRAY = (210, 210, 210)
BLACK      = (0, 0, 0)
YELLOW     = (170, 120, 0)
ORANGE     = (120, 70, 0)
BLUE       = (10, 10, 100)

class MainWindow:
	def __init__(self, title, width=640, height=480):
		pygame.init()
		self.width = width
		self.height = height
		
		self.screen = pygame.display.set_mode((self.width, self.height)) 
		pygame.display.set_caption(title)
		
		self.game = Game()
		self.humanPlayer = HumanPlayer(self.game, 'O')
		self.aiPlayer = AIPlayer(self.game, 'X')
		
		self.squareWidth = int(self.width/Game.boardWidth)
		self.squareHeight = int(self.height/Game.boardHeight)
		
		self.borderX = (self.width - Game.boardWidth*self.squareWidth)/2
		self.borderY = (self.height - Game.boardHeight*self.squareHeight)/2
		
		self.pieceSelected = None
		
		self.draw()
	
	def draw(self):
		self.drawBoard()
		self.drawPieces()
		
	def drawBoard(self):
		for y in range(Game.boardHeight):
			for x in range(Game.boardWidth):
				rect = Rect((self.borderX + x*self.squareWidth, self.borderY + y*self.squareHeight), 
							(self.squareWidth, self.squareHeight))
				if (x,y) in self.game.validMoves:
					pygame.draw.rect(self.screen, RED_WINE, rect)
				elif (x+y)%2 == 0:
					pygame.draw.rect(self.screen, YELLOW, rect)
				else:
					pygame.draw.rect(self.screen, ORANGE, rect)
				
				
	def drawPieces(self):
		radius = int(0.75 * min(self.squareWidth, self.squareHeight)/2)
		
		for y in range(Game.boardHeight):
			for x in range(Game.boardWidth):
				coord = (self.borderX + x*self.squareWidth + int(self.squareWidth/2),
						self.borderY + y*self.squareHeight + int(self.squareHeight/2))
				if self.isSelected((x,y)):
					pygame.draw.circle(self.screen, BLUE, coord, radius)
				elif self.game[x,y].upper() == 'O':
					pygame.draw.circle(self.screen, BLACK, coord, radius)
				elif self.game[x,y].upper() == 'X':
					pygame.draw.circle(self.screen, LIGHT_GRAY, coord, radius)
				
				if self.game[x,y].islower():
					pygame.draw.circle(self.screen, RED_WINE, coord, int(0.5*radius))
					
	def isSelected(self, pos):
		return self.pieceSelected != None and \
				(self.pieceSelected[0]==pos[0] and self.pieceSelected[1]==pos[1]) and \
				self.game[pos[0],pos[1]] != ' '
		
	def handleInput(self):
		for event in pygame.event.get():
			if event.type == QUIT:
				sys.exit()
			elif event.type == KEYDOWN and event.key == K_ESCAPE:
				sys.exit()
			elif event.type == MOUSEBUTTONDOWN:
				p = pygame.mouse.get_pos()
				self.mouseClick((p[0]/self.squareWidth, p[1]/self.squareHeight))
			elif event.type == MOUSEBUTTONUP:
				pass
				
	def mouseClick(self, pos):
		if pygame.mouse.get_pressed()[0]: # left button
			self.pieceSelected = self.humanPlayer['mouseClick'](self.pieceSelected, pos)
			self.draw()
			
	def iterate(self):
		if self.game.turn == self.aiPlayer['team']:
			self.aiPlayer['play']()
			self.draw()
		
	def run(self):
		while True:
			self.handleInput()
			self.iterate()
			pygame.display.flip()


if __name__ == "__main__":
	mainWindow = MainWindow("PyCheckers", 500, 500)
	mainWindow.run()
	
	

# background = pygame.Surface(screen.get_size())
# background = background.convert()
# background.fill((0, 0, 0))
# screen.blit(background, (0,0))
